<?php

namespace app\admin\model;

use think\Facade\Db;

class Teachermodel extends Commonmodel {

    protected $userTable = 'zhjs_user';
    protected $ctTable = 'zhjs_classroom_teacher';
    protected $croomTable = 'zhjs_classroom';

    public function __construct(array $data = [])
    {
        $this->table = 'zhjs_teacher';
        $this->pk = 'id';
        parent::__construct($data);
    }

    public function findAll($where = null, $pageParam = null, $num = 20){
        $whereData = [['t.is_delete' , '=', 1], ['u.is_delete' , '=', 1], ['c.is_delete' , '=', 1]];
        $paginate = ['list_rows'=>$num];

        if($where)  $whereData = array_merge($where, $whereData);
        if($pageParam = array_splice($pageParam, 1))  $paginate = array_merge($paginate, ['query'=>$pageParam]);

        $data = self::db()
            ->alias('t')
            ->leftJoin([$this->userTable=>'u'],'t.tuid=u.id')
            ->leftJoin([$this->croomTable=>'c'],'c.id=u.cid')
            ->field('t.id, t.nickname, u.status, u.username, t.create_date, c.name as cname')
            ->where($whereData)
            ->order(['t.id'=>'desc'])
            ->paginate($num);

        if(!$data)  return false;

        $list['page'] = $data->render();
        $dataArr = $data->toArray();
        $list['data'] = $dataArr['data'];

        return $list;
    }

    public function findDetaiById($id){
        if(!$id)  return false;

        $data = self::db()
            ->alias('t')
            ->leftJoin([$this->userTable=>'u'],'t.tuid=u.id')
            ->field('t.id, t.nickname, t.tuid, u.mobile, u.status, u.username, u.cid, t.desc')
            ->where(['t.id'=>$id])
            ->order(['t.id'=>'desc'])
            ->find()->toArray();

        return $data;
    }

    public function getteacherInfoByName($name, $id = null){
        if(!$name)  return false;

        if($id)  return self::db()->field('id')->where([[['nickname', '=', $name], ['id', '<>', $id]]])->find();
        return self::db()->field('id')->where(['nickname'=>$name])->find();
    }

    /**
     * 保存
     * @param $data
     * @param null $id
     * @return bool|string
     */
    public function saveTeacherInfo($data, $userData, $id = null){

        if(!$data)  return false;

        if(!$id){
            $data['uid'] = session('adminUserInfo.uid');
            $userData['create_date'] = $data['create_date'] = date("Y-m-d H:i:s", time());

            //添加教师信息
            $userData['rid'] = 2;
            $userData['admintype'] = 3;
        }

        $userData['password'] = $userData['password'] ? $userData['password'] : md5(md5($userData['password']));
        $userData['cid'] = $userData['cid'] ? $userData['cid'] : session('adminUserInfo.cid');

        Db::startTrans();
        try {
            if($id){
                if($data['cid'] != $userData['cid']){
                    Db::table($this->ctTable)->where(['cid'=>$data['cid'], 'tid'=>$id])->delete();
                    Db::table($this->ctTable)->save(['cid'=>$userData['cid'], 'tid'=>$id, 'create_date'=>$data['create_date']]);
                }
                Db::table($this->userTable)->where('id', $data['uid'])->update($userData);
                unset($data['cid'], $data['uid']);
                Db::table($this->table)->where($this->pk, $id)->update($data);
            }else {
                Db::table($this->userTable)->save($userData);
                $data['tuid'] = Db::table($this->userTable)->getLastInsID();
                Db::table($this->table)->save($data);
                Db::table($this->ctTable)->save(['cid'=>$userData['cid'], 'tid'=>Db::table($this->table)->getLastInsID(), 'create_date'=>$data['create_date']]);
                $id = Db::table($this->table)->getLastInsID();
            }
            Db::commit();
            return $id;
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }

}

?>